﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;

        if (!IsPostBack && Session["AdminId"]!=null)
        {
            TimeNow.Text= DateTime.Now.ToString();
            MyDataDataContext db = new MyDataDataContext();
            //获取用户信息
            var results = from r in db.SaleOfGoods
                          where r.GoodsID != null
                          select new
                          {
                              r.GoodsID,
                              r.GoodsName,
                              r.Price,
                              r.Type,
                              r.ReleaseTime,
                              r.OffShelfTime


                          };//映射需要显示的字段

            usergv.DataSource = results;
            //调用DataBind()方法显示信息
            usergv.DataBind();
        }
        else
        {
            Response.Redirect("../Login.aspx");
        }
    }

    protected void usergv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = usergv.Rows[e.RowIndex];
        MyDataDataContext db = new MyDataDataContext();
        SaleOfGoods DeleteObj = (from r in db.SaleOfGoods
                          where r.GoodsID == row.Cells[0].Text
                          select r).FirstOrDefault();
        //删除
        db.SaleOfGoods.DeleteOnSubmit(DeleteObj);
        db.SubmitChanges();
        string message = "删除成功！";
        string js = @"<Script language='JavaScript'>alert('" + message + "');window.close();</Script>";
        //获取用户信息
        var results = from r in db.SaleOfGoods
                      where r.GoodsID != null
                      select new
                      {
                          r.GoodsID,
                          r.GoodsName,
                          r.Price,
                          r.Type,
                          r.ReleaseTime,
                          r.OffShelfTime
                      };//映射需要显示的字段

        //设置GridView控件的数据源
        usergv.DataSource = results;
        //调用DataBind()方法显示信息
        usergv.DataBind();
        Response.Write(js);
        Page_Load(sender, e);
    }

}